﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;

namespace Portal_KonTum.Admin
{
    public partial class UserNew : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UrID"] = null;
            Session["UrID"] = Request.QueryString["UrID"];

            if (!IsPostBack)
            {
                iq = QueryMgr.GetDbInstance();
                List<Role> role = iq.Ro_GetAllRole();
                DL_Role.DataSource = role;
                DL_Role.DataValueField = "RoleID";
                DL_Role.DataTextField = "RoleName";
                DL_Role.DataBind();

                List<Identification> iden = iq.Iden_GetAllIdentification ();
                DL_Indentication.DataSource = iden;
                DL_Indentication.DataValueField = "IdentificationID";
                DL_Indentication.DataTextField = "IdentificationName";
                DL_Indentication.DataBind();

                List<Status> sta = iq.Sta_GetAllStatus();
                DL_status.DataSource = sta;
                DL_status.DataValueField = "StatusID";
                DL_status.DataTextField = "StatusName";
                DL_status.DataBind();

                if (Session["UrID"] == null)
                {

                }
                else
                {
                    bin_edit();
                    bt_save.Text = "Save";
                }
            }
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            if (txt_mk.Text.Length >= 5)
            {
                if (Session["UrID"] == null)
                {
                    // thêm mới
                    if (MethodCommon.IsHaveUserName(txt_tentk.Text))
                    {
                        iq.UR_NewUser(set_User());
                        Response.Redirect("UserMn.aspx?Notes=StrNew&Active=true");
                    }
                    else
                    {
                        lb_note.Text = "Đã tồn tại tên tài khoản này !";
                        lb_note.Enabled = true;
                    }
                }
                else
                {
                    // sua doi
                    Guid gd = MethodCommon.Get_Guid(Session["UrID"].ToString());
                    iq.UR_EditUser(set_User(gd));
                    Response.Redirect("UserMn.aspx?Notes=StrEdit&Active=true");
                }
            }
            else
            {
                Response.Write("<script> alert('Mật khẩu phải >= 5 ký tự !');</script>");
            }
        }

        protected void bt_cancel_Click(object sender, EventArgs e)
        {
            
        }

        void Fill_DataEdit(User bg)
        {
            txt_tentk.Enabled = false;
            txt_name.Text = bg.FullName;
            txt_mk1.Text = bg.Password;
            txt_tentk.Text = bg.UserName;
            DL_Role.SelectedValue = bg.RoleID.ToString();
            DL_status.SelectedValue = bg.StatusID.ToString();
            DL_Indentication.SelectedValue = bg.IdentificationID.ToString();
            txt_email.Text = bg.Mail;
            txt_giayto.Text = bg.IdentificationNumber;
            txt_phone.Text = bg.Phone;
            txt_diachi.Text = bg.Address;
        }

        void bin_edit()
        {
            User bg = new User();
            iq = QueryMgr.GetDbInstance();
            bg = iq.UR_GetUserByID(new Guid(Session["UrID"].ToString()));
            Fill_DataEdit(bg);
        }

        /// <summary>
        /// Sửa chữa bug
        /// </summary>
        /// <param name="bg"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        User set_User(Guid id)
        {
            User bg = new User();
            bg.RoleID = MethodCommon.Get_Guid(DL_Role.SelectedValue.ToString());
            bg.Password = MethodCommon.Md5AddSecret(txt_mk1.Text);
            bg.UserName = txt_tentk.Text;
            bg.FullName = txt_name.Text;
            bg.Address = txt_diachi.Text;
            bg.Phone = txt_phone.Text;
            bg.Mail = txt_email.Text;
            bg.IdentificationNumber = txt_giayto.Text;
            bg.IdentificationID = MethodCommon.Get_Guid(DL_Indentication.SelectedValue.ToString());
            bg.StatusID = MethodCommon.Get_Guid(DL_status.SelectedValue.ToString());
            bg.CreateDate = DateTime.Now;
            bg.IsDeleted = false;
            if (rd_sex.SelectedIndex == 0)
                bg.Sex = true;
            else
                bg.Sex = false;
            if (id != null) //sửa chữa
                bg.UserID = id;
            return bg;
        }

        /// <summary>
        /// thêm mới
        /// </summary>
        /// <param name="bg"></param>
        /// <returns></returns>
        User set_User()
        {
            User bg = new User();

            bg.RoleID = MethodCommon.Get_Guid(DL_Role.SelectedValue.ToString());
            bg.Password = MethodCommon.Md5AddSecret(txt_mk1.Text);
            bg.UserName = txt_tentk.Text;
            bg.FullName = txt_name.Text;
            bg.Address = txt_diachi.Text;
            bg.Phone = txt_phone.Text;
            bg.Mail = txt_email.Text;
            bg.IdentificationNumber = txt_giayto.Text;
            bg.IdentificationID = MethodCommon.Get_Guid(DL_Indentication.SelectedValue.ToString());
            bg.StatusID = MethodCommon.Get_Guid(DL_status.SelectedValue.ToString());
            bg.CreateDate = DateTime.Now;
            bg.IsDeleted = false;
            if (rd_sex.SelectedIndex == 0)
                bg.Sex = true;
            else
                bg.Sex = false;
            bg.UserID = Guid.NewGuid();
            return bg;
        }
    }
}